﻿using System.Collections.Generic;
using System.Net.DnsToolkit.Packet;
using System.Net.DnsToolkit.Serializer.Stream.Extension;
using System.Net.DnsToolkit.Packet.Records;
using System.Net.DnsToolkit.Packet.Busy;
using System.Net.DnsToolkit.Packet.Busy.Records;

namespace System.Net.DnsToolkit.Bastard.PacketGenerator {
    public class SelfRebindingAuthorityAdditional : IPacketGenerator {

        private readonly string _name;

        public SelfRebindingAuthorityAdditional() : this("darkdns.codeplex.com.") { }
        public SelfRebindingAuthorityAdditional(string name) {
            _name = name;
        }

        public byte[] GetBytes() {
            return GetMessage().GetBytes();
        }

        public IMessage GetMessage() {

            var message = new Message {
                Questions = new List<IQuestion> {
                    new Question {
                        QClass = QClassEnum.IN,
                        QType = QTypeEnum.A,
                        QName = new DomainName {
                            Name = _name
                        }
                    }
                },
                Additionals = new List<IRR> {
                    new RR {
                        Name = new DomainName{
                            Name = _name   
                        },
                        RRClass = QClassEnum.IN,
                        RRType = QTypeEnum.A,
                        Ttl = 10,
                        RDLength = 4,
                        RData = new RecordA {
                            Address = IPAddress.Parse("1.2.3.4")
                        }
                    }
                },
                Authorities = new List<IRR> {
                    new RR {
                        Name = new DomainName{
                            Name = _name   
                        },
                        RRClass = QClassEnum.IN,
                        RRType = QTypeEnum.NS,
                        Ttl = 10,
                        RDLength = 2,
                        RData = new RecordNS {
                            NSDNAME = new DomainName {
                                Name = _name
                            }
                        }
                    }
                },

            };

            message.Header.QuestionCount = (ushort)message.Questions.Count;
            message.Header.ResourceCount = (ushort)message.Additionals.Count;
            message.Header.AuthorityCount = (ushort)message.Authorities.Count;
            message.Header.AnswerCount = (ushort)message.Answers.Count;


            return message;


        }

    }
}
